package com.sky.aspect;

import com.sky.constant.AutoFillConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.enums.AutoFillType2;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import static com.sky.enums.AutoFillType2.*;

public class AutoFillType2Factory {

    /**
     * 策略集合
     * */
    private static Map<AutoFillType2, AutoFillStrategy> strategyMap = new HashMap<AutoFillType2, AutoFillStrategy>(){
        {
            put(INSERT_STATUS_DISABLE, new InsertStatusDisableStrategy());
            put(INSERT_STATUS_ENABLE, new InsertStatusEnableStrategy());
            put(UPDATE, new UpdateStrategy());
        }
    };

    /**
     * 工厂方法，获取填充策略
     * */
    public static AutoFillStrategy getAutoFillStrategy(AutoFillType2 type2){
       return strategyMap.get(type2);
    }

    /**
     * 填充策略1
     * */
    public static class InsertStatusDisableStrategy implements AutoFillStrategy{
        @Override
        public void autoFill(Object arg) throws Exception {
            Method setCreateTime = arg.getClass().getMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
            setCreateTime.invoke(arg, LocalDateTime.now());

            Method setCreateUser = arg.getClass().getMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
            setCreateUser.invoke(arg, BaseContext.getCurrentId());

            Method setUpdateTime = arg.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
            setUpdateTime.invoke(arg, LocalDateTime.now());

            Method setUpdateUser = arg.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
            setUpdateUser.invoke(arg, BaseContext.getCurrentId());

            Method setStatus = arg.getClass().getMethod(AutoFillConstant.SET_STATUS, Integer.class);
            setStatus.invoke(arg, StatusConstant.DISABLE);
        }
    }

    /**
     * 填充策略2
     * */
    public static class InsertStatusEnableStrategy implements AutoFillStrategy {
        @Override
        public void autoFill(Object arg) throws Exception {
            Method setCreateTime = arg.getClass().getMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
            setCreateTime.invoke(arg, LocalDateTime.now());

            Method setCreateUser = arg.getClass().getMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
            setCreateUser.invoke(arg, BaseContext.getCurrentId());

            Method setUpdateTime = arg.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
            setUpdateTime.invoke(arg, LocalDateTime.now());

            Method setUpdateUser = arg.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
            setUpdateUser.invoke(arg, BaseContext.getCurrentId());

            Method setStatus = arg.getClass().getMethod(AutoFillConstant.SET_STATUS, Integer.class);
            setStatus.invoke(arg, StatusConstant.ENABLE);
        }
    }

    /**
     * 填充策略3
     * */
    public static class UpdateStrategy implements AutoFillStrategy{
        @Override
        public void autoFill(Object arg) throws Exception {
            Method setUpdateTime = arg.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
            setUpdateTime.invoke(arg, LocalDateTime.now());

            Method setUpdateUser = arg.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
            setUpdateUser.invoke(arg, BaseContext.getCurrentId());
        }
    }


}
